<?php

/**
 * 广告投放管理MODEL类
 * 
 * @author Dean
 *
 */
class launchModel extends Model{
	
	//操作的本类表名
	private $tableName = 'launch';
	private $importtableName = 'importlaunch';
	private $adstableName = 'ads';
	
	/**
	 * 查询全部投放信息
	 */
	public function findAll($limit=0, $num=20){
		
		$sql = 'select * from '.tblName($this->tableName).'   ORDER BY createTime desc LIMIT '.$limit.' , '.$num;
		
		try {
			$userList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $userList;
	}
	
	/**
	 * 统计投放总数量
	 * 
	 * @param unknown_type $type
	 */
	public function countAll(){
		
		$sql = 'select count(*) as num from '.tblName($this->tableName);
		
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	/**
	 * 查询单个投放信息，通过投放编号进行查询
	 * 
	 * @param int $id 投放编号
	 */
	public function findOne($id){
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE ID = \''.$id.'\'';
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function findAllByType($type, $limit=-1, $num=20, $params=array()){
	
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= " AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			$select_by_sql .= " AND `keywords` like '%{$params['v_keywords']}%' ";
		}
		if (!empty($params['v_num'])){
			$select_by_sql .= " AND `num` like '%{$params['v_num']}%' ";
		}
		if (!empty($params['v_ads_id'])){
			$select_by_sql .= " AND `ads_id` like '%{$params['v_ads_id']}%' ";
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			$select_by_sql .= " AND `target_window` like '%{$params['v_target_window']}%' ";
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = "select * from ".tblName($this->tableName)."  WHERE type = '$type' $select_by_sql  ORDER BY createTime desc ";
		
		if($limit>=0){
			$sql = $sql." LIMIT $limit , $num";
		}
		 
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	/**
	 * 统计投放总数量
	 *
	 * @param unknown_type $type
	 */
	public function countAllByType($type, $params=array()){
	
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= " AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			$select_by_sql .= " AND `keywords` like '%{$params['v_keywords']}%' ";
		}
		if (!empty($params['v_num'])){
			$select_by_sql .= " AND `num` like '%{$params['v_num']}%' ";
		}
		if (!empty($params['v_ads_id'])){
			$select_by_sql .= " AND `ads_id` like '%{$params['v_ads_id']}%' ";
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			$select_by_sql .= " AND `target_window` like '%{$params['v_target_window']}%' ";
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = "select count(*) as num from ".tblName($this->tableName)." WHERE type = '$type'  $select_by_sql";
	
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	public function findEffectByType($type){
		
		$now = date('Y-m-d H:i:s');
		$sql = "select * from ".tblName($this->tableName)." WHERE type = '$type' AND (start <= '{$now}' AND end >= '{$now}') AND is_effect = '1' ORDER BY weight DESC  ";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
	
	/**
	 * 进行投放新增
	 * 
	 * @param Array $array
	 * @param int $returnID
	 */
	public function add($array, $returnID = 1){
		
		$sql = insertTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $this->db->lastInsertID;
		}else {
			throw new Exception('投放添加失败。');
			return -1;
		}
	}
	
	/**
	 * 进行修改操作
	 * 
	 * @param Array $array
	 * @param Array $where
	 */
	public function modify($array, $where){
		$sql = updateTableSql($this->tableName, $array, $where);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 * 
	 * @param Array $array
	 */
	public function delete($array){
		
		$sql = deleteTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 *
	 * @param Array $array
	 */
	public function import_delete($array){
	
		$sql = deleteTableSql($this->importtableName, $array);
	
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
	
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	public function import_findAll($limit=0, $num=20, $params){
		
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= " AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_ads_id'])){
			return array();
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			return array();
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = "select * from ".tblName($this->importtableName)." WHERE op<>'c' $select_by_sql  ORDER BY createTime desc LIMIT $limit ,$num";
		
		try {
			$userList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $userList;
	}
	
	public function import_findAllByType($type, $limit=-1, $num=20, $params=array()){
	
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= "AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			return array();
		}
		if (!empty($params['v_num'])){
			return array();
		}
		if (!empty($params['v_ads_id'])){
			return array();
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			return array();
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = "select * from ".tblName($this->importtableName)."  WHERE op<>'c' AND type = '$type' $select_by_sql ORDER BY createTime desc ";
	
		if($limit>=0){
			$sql = $sql." LIMIT $limit , $num";
		}
		
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function import_countAll(){
	
		$sql = 'select count(*) as num from '.tblName($this->importtableName);
	
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	public function import_countAllByType($type, $params=array()){
	
		$select_by_sql = '';
		if(!empty($params['v_id'])){
			$select_by_sql .= "AND `id` like '%{$params['v_id']}%'";
		}
		if (!empty($params['v_order'])){
			$select_by_sql .= " AND `order` like '%{$params['v_order']}%'";
		}
		if (!empty($params['v_activity'])){
			$select_by_sql .= " AND `activity` like '%{$params['v_activity']}%'";
		}
		if (!empty($params['v_name'])){
			$select_by_sql .= " AND `name` like '%{$params['v_name']}%' ";
		}
		if (!empty($params['v_launch_name'])){
			$select_by_sql .= " AND `launch_name` like '%{$params['v_launch_name']}%' ";
		}
		if (!empty($params['v_start'])){
			$select_by_sql .= " AND `start` like '%{$params['v_start']}%' ";
		}
		if (!empty($params['v_end'])){
			$select_by_sql .= " AND `end` like '%{$params['v_end']}%' ";
		}
		if (!empty($params['v_weight'])){
			$select_by_sql .= " AND `weight` like '%{$params['v_weight']}%' ";
		}
		if (!empty($params['v_keywords'])){
			return array();
		}
		if (!empty($params['v_num'])){
			return array();
		}
		if (!empty($params['v_ads_id'])){
			return array();
		}
		if (!empty($params['v_priority'])){
			$select_by_sql .= " AND `priority` like '%{$params['v_priority']}%' ";
		}
		if (!empty($params['v_target_window'])){
			return array();
		}
		if (!empty($params['v_size'])){
			$select_by_sql .= " AND `size` like '%{$params['v_size']}%' ";
		}
		
		$sql = "select count(*) as num from ".tblName($this->importtableName)." WHERE op<>'c' AND type = '$type' $select_by_sql ";
	
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	public function import_findOne($id){
	
		$sql = 'select * from '.tblName($this->importtableName).' WHERE ID = \''.$id.'\'';
	
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function import_modify($array, $where){
		$sql = updateTableSql($this->importtableName, $array, $where);
	
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
	
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	public function importDataToDB($fields, $arr){
	
		$sql = "replace into ".tblName($this->importtableName)." (".implode(',', $fields).") values ".implode(',', $arr)." ";
	
		$numRows = $this->db->execute($sql);
	
		if ($numRows > 0) {
			// 			return $this->db->lastInsertID;
			return $numRows;
		}else{
			throw new Exception('投放添加失败。');
			return -1;
		}
	}
	
	public function findByKws($type, $kws){
		
		$sql = "select * from ".tblName($this->tableName)."  WHERE type = '$type' AND keywords like '%{$kws}%'  ORDER BY createTime desc ";
		
		try {
			$adsList = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $adsList;
	}
	
	public function findByArticleID($articleID){
		$now = date('Y-m-d H:i:s');
		$sql = "select * from tbl_launch_group  WHERE articles like '%{$articleID}%' AND (start <= '{$now}' AND end >= '{$now}') ORDER BY weight DESC  limit 1 ";
		
		try {
			$launchInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $launchInfo;
	}
	
	public function findKWByArticleID($articleID){
		$now = date('Y-m-d H:i:s');
		$sql = "select * from tbl_launch  WHERE articles like '%{$articleID}%' AND type='keywords' AND (start <= '{$now}' AND end >= '{$now}') ORDER BY weight DESC  limit 1 ";
	
		try {
			$launchInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $launchInfo;
	}
	
	public function addImg($id, $imgArr){
		$sql = "replace into tbl_launch_img (`id`, `img`, `img_title`, `img_desc`, `img_outurl`, `img_price`,  `createtime`) values ('$id','{$imgArr['img']}', '{$imgArr['img_title']}', '{$imgArr['img_desc']}', '{$imgArr['img_outurl']}', '{$imgArr['img_price']}','".date('Y-m-d H:i:s')."')";
		
		$numRows = $this->db->execute($sql);
		if ($numRows > 0) {
			return $numRows;
		}else{
			throw new Exception('插入投放图片失败。');
			return -1;
		}
	}
	
	public function updateImg($id, $imgArr){
		$sql = "update tbl_launch_img set `img` = '{$imgArr['img']}', `img_title` = '{$imgArr['img_title']}', `img_desc` = '{$imgArr['img_desc']}', `img_outurl` = '{$imgArr['img_outurl']}', `img_price` = '{$imgArr['img_price']}' where id = '$id'";
		
		$numRows = $this->db->execute($sql);
		if ($numRows >= 0) {
			return $numRows;
		}else{
			throw new Exception('修改投放图片失败。');
			return -1;
		}
	}
	
	public function findImgByID($id){
		
		$sql = "select * from tbl_launch_img where id = '$id' ";
		
		try {
			$imgInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $imgInfo;
	}
	
	public function findLaunchIDByHDID($id){
		
		$sql = "select t1.* from ".tblName($this->importtableName)." t1 left join ".tblName($this->adstableName)." t2 on t1.name = t2.launch_name where t2.id='$id'";
		
		try {
			$imgInfo = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $imgInfo;
	}
	
	public function getMaxID(){
	
		$sql = "select id from ".tblName($this->tableName)." where id like 'HD%' order by id desc limit 1";
	
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function getKWImgByID($id){
		$sql = "select t1.*, t2.tpl from tbl_launch_img t1 left join tbl_launch t2 on t1.id = t2.id where t2.id = '$id' limit 1";
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function findImportVBCLaunchByName($name){
		
		$sql = 'select * from '.tblName($this->importtableName).' WHERE name = \''.$name.'\'';
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
}


?>